﻿// (c) Copyright lexun
// This source is subject to the Lexun zhangqinghe
using System;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Phone.Controls.Primitives;
using Microsoft.Phone.Controls;

namespace PhoneInfo
{
    [TemplatePart(Name = HeadersListElement, Type = typeof(PivotHeadersControl))]
    [TemplatePart(Name = PivotItemPresenterElement, Type = typeof(ItemsPresenter))]
    [StyleTypedProperty(Property = ItemContainerStyleName, StyleTargetType = typeof(PivotItem))]
    public class LexunPivot : Pivot
    {

        /// <summary>
        /// Headers list element template part.
        /// </summary>
        private const string HeadersListElement = "HeadersListElement";

        /// <summary>
        /// Content element template part.
        /// </summary>
        private const string PivotItemPresenterElement = "PivotItemPresenter";

        /// <summary>
        /// The item container style property name.
        /// </summary>
        internal const string ItemContainerStyleName = "ItemContainerStyle";
        //
        private PivotHeadersControl _header = null;
        //
        private static Duration _animTime =
          new Duration(TimeSpan.FromMilliseconds(10));

        private const double _animOffset = -20.0;
        public LexunPivot()
        {
            SelectionChanged += OnSelectionChanged;
        }
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_header == null)
            {
                _header = FindHeader(this);
               
            }
            foreach (PivotHeaderItem headerItem in _header.Items)
            {
                if (!headerItem.IsSelected)
                {
                    headerItem.FontSize = 36;
                    //
                    
                    
                    //TransformAnimator transformAnimator = 
                    //headerItem.RenderTransformOrigin = new Point(0, 20);
                    //
                    //TranslateTransform
                    //
                   // headerItem.Height = 60;
                  //  headerItem.Margin = new Thickness(20);
                   // headerItem.HorizontalAlignment = HorizontalAlignment.Center;
                  //  headerItem.HorizontalAlignment = HorizontalAlignment.Center;
                    //headerItem.Padding.Left = 20;
                    
                  
                }
                else
                {
                    headerItem.FontSize = 48;
                   // headerItem.RenderTransformOrigin = new Point(-0.5, -0.5);
                   // headerItem.Width = 120;
                   // TranslateTransform transform = TransformAnimator.GetTranslateTransform(headerItem);
                   // TransformAnimator transformAnimator = new TransformAnimator(transform);
                   // transformAnimator.GoTo(
                   //_animOffset + transform.X,
                   //_animTime
                   // );
                   // headerItem.Height = 60;
                   // headerItem.HorizontalAlignment = HorizontalAlignment.Center;
                   // headerItem.HorizontalAlignment = HorizontalAlignment.Center;
                   
                }
            }

        }
        private PivotHeadersControl FindHeader(UIElement start)
        {
            UIElement target = null;

            int childCount = VisualTreeHelper.GetChildrenCount(start);

            for (int i = 0; i < childCount; i++)
            {
                UIElement e = VisualTreeHelper.GetChild(start, i) as UIElement;

                if (e is PivotHeadersControl)
                {
                    target = e;
                }
                else
                {
                    target = FindHeader(e);
                }

                if (target != null)
                {
                    break;
                }
            }

            return target as PivotHeadersControl;
        }

    }
}
